<?php 
declare(strict_types=1);
namespace App\Service;

 use App\Constants\ErrorCode;
 use App\Dao\AdminLogDao;
 use App\Dao\OilAmountChannelDao;
 use App\Dao\OilAmountDao;
 use App\Dao\OilChannelDao;
 use App\Exception\BusinessException;

 use App\Utils\ServerUtil;

 class OilChannelService
 {
     /**
      * 获取合作商列表
      * @param $page
      * @param $pageSize
      * @param string $name
      * @return array
      */
     public function list($page, $pageSize, $name='')
     {

         $where = ['is_del' => 1];
         if($name){
             $where[] = ['name','like' ,'%'.$name.'%'];
         }
         return make(OilChannelDao::class)->getListPage($where, $page, $pageSize);

     }

     /**
      * 获取全部合作商
      * @return array
      */
     public function all()
     {
         return make(OilChannelDao::class)->getAll(['id','name']);
     }

     /**
      * 添加合作商
      * @param $insert
      * @return int
      */
     public function add($insert)
     {
         $apiInfoArray = @json_decode($insert['api_info'],true);
         if(empty($apiInfoArray['api_name'])){
             throw new BusinessException(ErrorCode::ERR_SERVER, '合作商api信息参数不完整');
         }
         $insert['start_time'] = strtotime($insert['start_time']);
         $insert['id'] = make(OilChannelDao::class)->add($insert);
         if(empty($insert['id'])){
             throw new BusinessException(ErrorCode::ERR_SERVER, '网络繁忙,请重试');
         }
         make(AdminLogDao::class)->addLog('添加合作商：'.json_encode($insert));
         return $insert['id'];
     }

     /**
      * 获取合作商信息
      * @param $id
      * @return array
      */
     public function get($id)
     {
         $info = make(OilChannelDao::class)->findById($id, ['*'], true);
         if(empty($info)){
             throw new BusinessException(ErrorCode::ERR_BUESSUS, '合作商不存在');
         }
         return $info;
     }


     /**
      * 更新合作商信息
      * @param $update
      * @return int
      */
     public function edit($update)
     {

         $apiInfoArray = @json_decode($update['api_info'],true);
         if(empty($apiInfoArray['api_name'])){
             throw new BusinessException(ErrorCode::ERR_SERVER, '合作商api信息参数不完整');
         }

         $channelDao = make(OilChannelDao::class);
         $info = $channelDao->findById($update['id'], ['*'], true);
         if(empty($info)){
             throw new BusinessException(ErrorCode::ERR_BUESSUS, '合作商不存在');
         }

         make(AdminLogDao::class)->addLog('编辑合作商：编辑前：'.json_encode($info).'  编辑后：'.json_encode($update));
         unset($update['id']);
         $update['start_time'] = strtotime($update['start_time']);
         return $channelDao->updateById($info['id'], $update);
     }

     /**
      * 删除合作商
      * @param $id
      * @return int
      */
     public function del($id)
     {
         $channelDao = make(OilChannelDao::class);
         $info = $channelDao->findById($id, ['id'], true);
         if(empty($info)){
             throw new BusinessException(ErrorCode::ERR_BUESSUS, '合作商不存在');
         }
         make(AdminLogDao::class)->addLog('删除合作商：'.json_encode($info));
         return $channelDao->updateById($id, ['is_del'=>2]);
     }

     /**
      * 编辑合作商备注
      * @param $id
      * @param $remark
      * @return int
      */
     public function remark($id, $remark)
     {
         $channelDao = make(OilChannelDao::class);
         $info = $channelDao->findById($id, ['id','remark'], true);
         if(empty($info)){
             throw new BusinessException(ErrorCode::ERR_BUESSUS, '合作商不存在');
         }
         make(AdminLogDao::class)->addLog('编辑合作商备注：编辑前：'.json_encode($info).'  编辑后：'.$remark);
         return $channelDao->updateById($id, ['remark'=>$remark]);
     }


     /**
      * 获取合作商充值金额
      * @param $id
      * @return array
      */
     public function rechargeAmount($id)
     {
         $rechargeAmountChannelDao = make(OilAmountChannelDao::class);
         $info = make(OilChannelDao::class)->findById($id, ['*'], true);
         if(empty($info)){
             throw new BusinessException(ErrorCode::ERR_BUESSUS, '合作商不存在');
         }

         $where = ['status' => 1, 'type_id'=>$info['type']];

         $list = make(OilAmountDao::class)->searchByWhere($where,['id','sort','type_id as type', 'ori_amount as amount']);
         if(empty($list)){
             throw new BusinessException(ErrorCode::ERR_BUESSUS, '请先配置充值面值');
         }
         foreach ($list as $k => $v){
             $cost = $rechargeAmountChannelDao->costByWhere(['amount_id'=>$v['id'], 'channel_id'=>$id]);
             $list[$k]['cost'] = $cost ? $cost : 0;
         }

         return $list;
     }

     /**
      * 编辑合作商充值金额
      * @param $id
      * @param $rechargeAmount
      * @return bool
      */
     public function rechargeAmountEdit($id, $rechargeAmount)
     {
         $channelDao = make(OilChannelDao::class);
         $amountChannelDao = make(OilAmountChannelDao::class);

         $info = $channelDao->findById($id, ['*'], true);
         if(empty($info)){
             throw new BusinessException(ErrorCode::ERR_BUESSUS, '合作商不存在');
         }

         $beforeList = $amountChannelDao->searchByWhere(['channel_id'=>$id]);
         ServerUtil::info( '编辑合作商充值金额', [
             'path'=>'/admin/channel_rechargeAmount',
             'parameter'=>['id'=>$id,'recharge_amount'=>$rechargeAmount],
             'beforeList'=> $beforeList,
         ]);

         $insert = [];
         $insert['channel_id'] = $id;
         foreach ($rechargeAmount as $value) {
             if(empty($value['amount_id'])||empty($value['cost'])){
                 throw new BusinessException(ErrorCode::ERR_BUESSUS, '合作商充值金额参数不完整');
             }
             $insert['amount_id'] = $value['amount_id'];
             if(!empty($amountChannelDao->searchByWhere($insert, ['id']))){
                 $amountChannelDao->update($insert, ['cost'=>$value['cost']]);
             }else{
                 $insert['cost'] = $value['cost'];
                 $amountChannelDao->add($insert);
                 unset($insert['cost']);
             }
         }

         $list = $amountChannelDao->searchByWhere(['channel_id'=>$id]);
         make(AdminLogDao::class)->addLog('编辑合作商成本备注：编辑前：'.json_encode($beforeList).'  编辑后：'.json_encode($list));
         return true;
     }


     /**
      * 面值获取支持的合作商信息
      * @param $oriAmount
      * @param $amount
      * @param $type
      * @return \Hyperf\Database\Model\Builder|\Hyperf\Database\Model\Model|object|null
      */
     public function amountByChannelInfo($oriAmount, $amount, $type){

         //获取充值面值信息
         $rechargeAmountInfo = make(OilAmountDao::class)->isLegalDisCountAmount($oriAmount, $amount, $type);
         if(empty($rechargeAmountInfo)){
             throw new BusinessException(ErrorCode::ERR_BUESSUS, '充值面值不支持');
         }

         $channelInfo = make(OilChannelDao::class)->findChannelByAmountArea($rechargeAmountInfo['id']);
         if(empty($channelInfo)){
             throw new BusinessException(ErrorCode::ERR_BUESSUS, '没有可用合作商');
         }
         $channelInfo['item_id'] = $rechargeAmountInfo['item_id'];
         return $channelInfo;

     }

     /**
      * 根据商户号获取合作商信息
      * @param $merchantNo
      * @return \Hyperf\Database\Model\Builder|\Hyperf\Database\Model\Model|object|null
      */
     public function merchantNoByChannelInfo($merchantNo){
         return make(OilChannelDao::class)->merchantNoByChannelInfo($merchantNo);
     }
 }